'''
@Author: your name
@Date: 2019-06-24 11:32:40
LastEditTime: 2022-05-22 16:14:02
LastEditors: 风云 mayude@live.cn
@Description: In User Settings Edit
@FilePath: /tinyms/tinyms-api/apps/cms/link/views.py
'''
from flask import render_template,request,jsonify
from . import cms_article
from .model import Article
from .service import ArticleService
from apps.cms.articlechannel.service import ChannelArticleService
from apps.cms.articlechannel.model import ChannelArticle
from apps.utils import auty
import json
import uuid
import time

'''
保存或修改文章
'''
@cms_article.route('/saveOrUpdate',methods=['GET','POST'])
@auty.login_required
def save():
    data = {}
    if request.method == "POST":
        for key in request.form:
            data[key] = request.form.get(key)
    else:
        for key in request.args:
            data[key] = request.args.get(key)
    article = Article()
    article.article_title = data['article_title']
    article.article_titleimg = ""
    # article.article_ftitle = ""
    article.article_sendtime = data['article_sendtime']
    article.article_author = ""
    # article.article_source = data['article_source']
    article.article_istop = ""
    article.article_state = data['article_state']
    article.article_content = data['article_content']
    # article.article_type = data['article_type']
    # article.article_click = data['article_click']
    article.article_url = ""
    article.article_createtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    channel_pks = data['channel_pks']
    if not data['article_pk']:
        # article.article_pk = str(uuid.uuid1())
        #保存文章
        ArticleService.save(article)
        #保存文章与栏目间的关系
        tmp = channel_pks.split(',')
        for pk in tmp:
            channelArticle = ChannelArticle()
            channelArticle.fk_article_pk = article.article_pk
            channelArticle.fk_channel_pk = pk
            ChannelArticleService.save(channelArticle)
    else:
        article.article_pk = data['article_pk']
        # article.article_upauthor = data['article_upauthor']
        article.article_uptime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        ArticleService.update(article)
        #删除原来的栏目与文章的关系
        ChannelArticleService.delete(article.article_pk)
        tmp = channel_pks.split(',')
        for pk in tmp:
            channelArticle = ChannelArticle()
            channelArticle.fk_article_pk = article.article_pk
            channelArticle.fk_channel_pk = pk
            ChannelArticleService.save(channelArticle)
    data = {
        'msg':'保存成功',
        'code':'0',
    }
    return json.dumps(data,ensure_ascii=False)


'''
分页查询文章
'''
@cms_article.route('/pages',methods=['GET','POST'])
@auty.login_required #必须登录的装饰器校验
def pages():
    if request.method == "POST":
        page = request.form.get("page") #获取页码
        limit = request.form.get("limit") #获取页码
        article_title = request.form.get("article_title") #获取页码
    else:
        page = request.args.get("page")
        limit = request.args.get("limit")
        article_title = request.args.get("article_title") #获取页码
    article = Article()  
    if(article_title is not None):
        article.article_title = article_title
    pages = ArticleService.pages(article,page,limit)
    # db.session.close()
    #将分页对象组装为需要的数据
    data = {
        'msg':'success',
        'code':0,
        'data':Article.to_dict(pages.items),
        'total':pages.total,
        'pageNum':pages.page,
        'pageSize':pages.per_page
    }
    print(pages.items)
    return json.dumps(data,ensure_ascii=False)
    # return jsonify(data)


'''
通过主键删除
'''
@cms_article.route('/delete',methods=['GET','POST'])
@auty.login_required
def delete():
    if request.method == "POST":
        article_pk = request.form.get("article_pk")
    else:
        article_pk = request.args.get("article_pk")
    articles = ArticleService.delete(article_pk)
    data = {
        'msg':'success',
        'code':0,
        'data':""
    }
    return json.dumps(data)

'''
文章撤回
'''
@cms_article.route('/recall',methods=['GET','POST'])
@auty.login_required #必须登录的装饰器校验
def recall():
    if request.method == "POST":
        article_pk = request.form.get("article_pk") #获取页码
        article_state = request.form.get("article_state") #获取页码
    else:
        article_pk = request.args.get("article_pk")
        article_state = request.args.get("article_state")
    article = Article()
    article.article_pk = article_pk
    article.article_state = article_state
    ArticleService.update(article)
    # db.session.close()
    #将分页对象组装为需要的数据
    data = {
        'msg':'撤回成功',
        'code':0,
        'data':"",
    }
    return json.dumps(data,ensure_ascii=False)

'''
文章发布
'''
@cms_article.route('/send',methods=['GET','POST'])
@auty.login_required #必须登录的装饰器校验
def send():
    if request.method == "POST":
        article_pk = request.form.get("article_pk") #获取文章住建
        article_state = request.form.get("article_state") #获取文章状态
    else:
        article_pk = request.args.get("article_pk")
        article_state = request.args.get("article_state")
    article = Article()
    article.article_pk = article_pk
    article.article_state = article_state
    ArticleService.update(article)
    # db.session.close()
    #将分页对象组装为需要的数据
    data = {
        'msg':'发布成功',
        'code':0,
        'data':"",
    }
    return json.dumps(data,ensure_ascii=False)









